package com.atguigu.vod.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.commonutils.R;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.vod.Utils.ConstantVodUtils;
import com.atguigu.vod.Utils.InitObject;
import com.atguigu.vod.Utils.InitVodClient;
import com.atguigu.vod.service.VodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


@Api(description="阿里云视频操作")
@RestController
@RequestMapping("/eduvod/video")//指定路径
@CrossOrigin
public class VodController {

    @Autowired//注入接口
    private VodService vodService;

    //上传视频到阿里云
    @ApiOperation(value = "上传视频")
    @PostMapping("uploadAlyiVideo")
    public R uploadAlyiVideo(MultipartFile file) {//MultipartFile获取到上传的文件
        //返回上传视频id
        String videoId = vodService.uploadVideoAly(file);
        System.out.println("视频id:"+videoId);
        return R.ok().data("videoId",videoId);
    }
    //根据视频id删除阿里云中的视频
    @ApiOperation(value = "删除单个视频")
    @DeleteMapping("/removeAiyVideo/{id}")
    public R removeAiyVideo(@PathVariable String id) {


        try {
            //初始化对象
            DefaultAcsClient client = InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //创建删除视频request对象
            DeleteVideoRequest request = new DeleteVideoRequest();
            //向request设置视频id
            request.setVideoIds(id);
            //调用初始化对象的方法实现删除
            client.getAcsResponse(request);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            throw new GuliException(20001, "删除视频失败");
        }

    }

    //删除多个阿里云中的视频方法
    //参数多个视频id List videoIDList
    @ApiOperation(value = "删除多个视频")
    @DeleteMapping("delete-batch")
    public R deleteBacht(@RequestParam("videoIdList") List<String> videoIdList) {
        vodService.removeMoreAlyVideo(videoIdList);//在VodService接口中定义方法
        return R.ok();
    }

    //根据视频id，获取视频的凭证
    @GetMapping("getPlayAuth/{id}")
    public R getPlayAuth(@PathVariable String id) {
        try {
            //1.创建初始化对象
            DefaultAcsClient Client =
                    InitVodClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            //2.创建获取视频地址request和response
            GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
            //GetVideoPlayAuthResponse response = new GetVideoPlayAuthResponse();
            //3.向request对象里面设置视频id
            request.setVideoId(id);
            //4.调用初始化对象里面的方法，传递request，获取数据
            GetVideoPlayAuthResponse response = Client.getAcsResponse(request);
            //5.通过response得到凭证
            String playAuth = response.getPlayAuth();
            System.out.println("视频的凭证playAuth:"+playAuth);
            return R.ok().data("playAuth",playAuth);
        } catch (Exception e) {
            throw new GuliException(20001,"获取凭证失败");
        }

    }

}
